package com.hancher.gamelife2.account;

import android.os.Bundle;

import androidx.navigation.Navigation;
import androidx.sqlite.db.SimpleSQLiteQuery;

import com.hancher.common.androidutil.AsyncUtils;
import com.hancher.common.androidutil.ToastUtil;
import com.hancher.common.base.BaseFragment;
import com.hancher.common.javautil.DecryptUtil;
import com.hancher.common.javautil.EmptyUtil;
import com.hancher.gamelife2.databinding.FragmentAccountEditBinding;
import com.hancher.gamelife2.main.MainDatabase_Impl;

import java.util.List;

import io.reactivex.rxjava3.functions.Consumer;

/**
 * 作者 : Hancher ytu_shaoweijie@163.com <br/>
 * 时间 : 2023/3/12 0012 22:49 <br/>
 * 描述 :
 */
public class AccountEditFragment extends BaseFragment<FragmentAccountEditBinding> {

    String recordUuid;
    AccountPO accountPO;

    @Override
    protected void initView(Bundle savedInstanceState) {
        if (getArguments() != null) {
            recordUuid = getArguments().getString("data");
        }
    }

    @Override
    protected void initData() {
        if (EmptyUtil.isEmpty(recordUuid)) {
            accountPO = new AccountPO();
            updateData();
        } else {
            AsyncUtils.run(emitter -> {
                String sql = " SELECT * FROM account  WHERE deleteflag!=1 AND uuid='" + recordUuid + "' ";
                List<AccountPO> poList = MainDatabase_Impl.getInstance().accountDao().getList(new SimpleSQLiteQuery(sql));
                if (EmptyUtil.isNotEmpty(poList)) {
                    emitter.onNext(poList.get(0));
                }
            }, (Consumer<AccountPO>) data -> {
                accountPO = data;
                updateData();
            });
        }
    }

    @Override
    protected void initListener() {
        binding.btnFloat.setOnClickListener(v -> {
            accountPO.setUpdateTime(System.currentTimeMillis());
            accountPO.setDeleteflag((byte) 0);
            accountPO.setAccount(binding.editAccount.getText().toString());
            accountPO.setUser(binding.editName.getText().toString());
            accountPO.setPsd(DecryptUtil.encrypt2(binding.editPsw.getText().toString()));
            accountPO.setNote(binding.editDescription.getText().toString());
            AsyncUtils.run(emitter -> {
                if (EmptyUtil.isEmpty(recordUuid)) {
                    MainDatabase_Impl.getInstance().accountDao().insertItem(accountPO);
                    emitter.onNext("");
                } else {
                    MainDatabase_Impl.getInstance().accountDao().updateItem(accountPO);
                    emitter.onNext("已更新");
                }

            }, (Consumer<String>) o -> {
                if (EmptyUtil.isEmpty(o)) {
                    Navigation.findNavController(binding.container).popBackStack();
                } else {
                    ToastUtil.show(o);
                }
            });
        });
    }

    private void updateData() {
        binding.editAccount.setText(accountPO.getAccount());
        binding.editName.setText(accountPO.getUser());
        if (EmptyUtil.isNotEmpty(accountPO.getPsd())) {
            binding.editPsw.setText(DecryptUtil.decrypt2(accountPO.getPsd()));
        }
        binding.editDescription.setText(accountPO.getNote());
    }
}